{-------------------------------------------------------------------------------
  Author: Duo (iamduo@qq.com)
  DateTime: 2019/7/13 22:19:53

  ADODB.TParameters
-------------------------------------------------------------------------------}
unit uDBParam_Parameters;

interface

uses uDB_Base, uBizDBIntf, SysUtils, ADODB, Variants;

type
  TDBParam_Parameter = class(TDBLockedBase, IDBParam)
  private
    FParameter:TParameter;
  public
    constructor Create(const ADBParamsIntf:IDBParams; const AParameter:TParameter);
    destructor Destroy;override;
    { IDBParam }
    function IsBlob:Boolean;
    function IsNull:Boolean;
    function Name:WideString;
    function Kind:WideString;
    function Size:Integer;
    function GetValue:OleVariant;
    procedure SetValue(const AValue:OleVariant);
    property Value:OleVariant read GetValue write SetValue;
  end;

  TDBParam_Parameters = class(TDBLockedBase, IDBParams)
  private
    FParameters:TParameters;
  public
    constructor Create(const ADBDataIntf:IDBData; const AParameters:TParameters);
    destructor Destroy;override;
    { IDBParams }
    function Count:Integer;
    function Add:Integer;
    function Delete(const AIndex:Integer):Boolean;
    function GetParams(const AIndex:Integer):IDBParam;
    procedure SetParams(const AIndex:Integer; const AValue:IDBParam);
    property Params[const AIndex:Integer]:IDBParam read GetParams write SetParams;default;
  end;

implementation

uses Classes;

{ TDBParam_Parameter }

constructor TDBParam_Parameter.Create(const ADBParamsIntf: IDBParams;
  const AParameter: TParameter);
begin
  inherited Create(ADBParamsIntf);
  FParameter := AParameter;
end;

destructor TDBParam_Parameter.Destroy;
begin
  FParameter := nil;
  inherited;
end;

function TDBParam_Parameter.GetValue: OleVariant;
begin
  Result := FParameter.Value;
end;

function TDBParam_Parameter.IsBlob: Boolean;
begin
  Result := FieldType_IsBlob(FParameter.DataType);
end;

function TDBParam_Parameter.IsNull: Boolean;
begin
  Result := VarIsNull(FParameter.Value);
end;

function TDBParam_Parameter.Kind: WideString;
begin
  Result := FieldType_ToString(FParameter.DataType);
end;

function TDBParam_Parameter.Name: WideString;
begin
  Result := FParameter.Name;
end;

procedure TDBParam_Parameter.SetValue(const AValue: OleVariant);
begin
  FParameter.Value := AValue;
end;

function TDBParam_Parameter.Size: Integer;
begin
  Result := FParameter.Size;
end;

{ TDBParam_Parameters }

function TDBParam_Parameters.Add: Integer;
begin
  Lock;
  try
    Result := FParameters.Count;
    FParameters.Add;
  finally
    UnLock;
  end;
end;

function TDBParam_Parameters.Count: Integer;
begin
  Lock;
  try
    Result := FParameters.Count;
  finally
    UnLock;
  end;
end;

constructor TDBParam_Parameters.Create(const ADBDataIntf: IDBData;
  const AParameters: TParameters);
begin
  inherited Create(ADBDataIntf);
  FParameters := AParameters;
end;

function TDBParam_Parameters.Delete(const AIndex: Integer): Boolean;
begin
  Lock;
  try
    Result := false;
    if (AIndex >= 0) and (AIndex < FParameters.Count) then
    begin
      FParameters.Delete(AIndex);
      Result := true;
    end;
  finally
    UnLock;
  end;
end;

destructor TDBParam_Parameters.Destroy;
begin
  FParameters := nil;
  inherited;
end;

function TDBParam_Parameters.GetParams(const AIndex: Integer): IDBParam;
begin
  Lock;
  try
    Result := TDBParam_Parameter.Create(Self, FParameters[AIndex]);
  finally
    UnLock;
  end;
end;

procedure TDBParam_Parameters.SetParams(const AIndex: Integer;
  const AValue: IDBParam);
begin
  Lock;
  try
    FParameters[AIndex].Name := AValue.Name;
    FParameters[AIndex].DataType := FieldType_FromString(AValue.Kind);
    FParameters[AIndex].Size := AValue.Size;
    FParameters[AIndex].Value := AValue.Value;
  finally
    UnLock;
  end;
end;

end.

